
package com.yuan.localServer;

import com.amap.api.location.AMapLocation;
import com.yuan.unit.BLog;

import java.util.ArrayList;

/**
 * 定位服务管理
 * 
 * @author YUAN
 */
public class LocalManage {
    private volatile static LocalManage sInstance;
    private ArrayList<OnMyLocalChange> mListeners;
    public double lng = 0.0, lat = 0.0;
    public String adress = "";

    private LocalManage() {
        mListeners = new ArrayList<OnMyLocalChange>();
    }

    public static LocalManage getInstance() {
        if (sInstance == null) {
            synchronized (LocalManage.class) {
                if (sInstance == null) {
                    sInstance = new LocalManage();
                }
            }
        }
        return sInstance;
    }

    public final void registerApplicationDestroyListener(OnMyLocalChange lis) {
        if (lis != null && !mListeners.contains(lis)) {
            mListeners.add(lis);
        }
    }

    public final void unRegisterApplicationDestroyListener(OnMyLocalChange lis) {
        if (lis != null && mListeners.contains(lis)) {
            mListeners.remove(lis);
        }
    }

    public void onNoticeLocalChanged(AMapLocation location) {
        // first in, destroy last
        for (int i = mListeners.size() - 1; i > -1; i--) {
            OnMyLocalChange lis = mListeners.get(i);
            try {
                BLog.d(LocalManage.class.getSimpleName(),
                        "Calling LoaclChanged for " + lis.getClass().getName());
                lis.onLocalChanged(location);
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }
}
